package com.gsafety.wordtiles.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * 基础入口配置
 * @author xdh
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private Environment environment = null;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
                allowedOriginPatterns("*").
                allowedMethods("POST", "GET", "OPTIONS").
                maxAge(3600).
                allowCredentials(true);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //模型结果文件路径
        String outputDir = environment.getProperty("wordTiles.path");
        if (outputDir != null) {
            String[] outputDirList = outputDir.split(",");
            for (int i = 0; i < outputDirList.length; i++) {
                outputDirList[i] = "file:///" + outputDirList[i];
            }
            registry.addResourceHandler("/wordTiles/**")
                    .addResourceLocations(outputDirList);
        }
    }

}
